var func = require('../../utils/func.js');
var net = require('../../utils/ajax.js');
var url = require('../../utils/url.js');
var user = require('../../utils/user.js');
var Promise = require('../../libs/promise.js');

let me;
let aid;
let uid;
let draft={};

let loadData = () => {
    return new Promise((s,f) => {
        getUsers().then(() => {
            getDraft()
        }).then(() => {
            s();
        })
    })
};

let getUsers = () => {
    return net.send(url.getViewers,{
        uid:uid,
        rid:aid
    }).then(res => {
        let now = new Date().getTime()/1000;

        let content = res.info;

        for(let i = 0;i<content.length;i++){
            if((now - content[i].create_time)<60){
                content[i].time = (now - content[i].create_time).toString() + "秒前";
            }else if((now - content[i].create_time)<3600){
                content[i].time = parseInt((now - content[i].create_time)/60).toString() + "分钟前";
            }else if((now - content[i].create_time)>3600 || (now - content[i].create_time)==3600){
                content[i].time = parseInt((now - content[i].create_time)/3600).toString() + "小时前";
            }
        }

        me.setData({
            users:content
        })
    })
};

let getDraft = () => {
    return net.send(url.getArticleDraft,{
        rid:aid
    }).then(res => {
        if(res.status=="y"){
            console.log(res);
            transformContent(res);
        }
    })
};

let transformContent = (res) => {
    let content = res.info;
    let list = content.list;
    let contentParse = [];

    if(!list){
        me.setData({
            title:res.info.title,
            id:res.info.id
        })
        return;
    }

    for(let i = 0;i<list.length;i++){
        let obj = {};
        if(list[i].type == "标题"){
            obj = {
                index:i,
                type:"title",
                typename:"标题",
                text:list[i].text,
                id:"title_"+i.toString()
            }
        }else if(list[i].type == "文本"){
            obj = {
                index:i,
                type:"text",
                typename:"文本",
                text:list[i].text,
                id:"text_"+i.toString()
            }
        }else if(list[i].type == "图片"){
            obj = {
                index:i,
                type:"image",
                typename:"图片",
                text:list[i].text,
                removeSrc:list[i].src,
                maskText:"",
                id:"image_"+i.toString()
            }
        }else if(list[i].type == "分割线"){
            obj = {
                index:i,
                type:"line",
                typename:"分割线",
                id:"line_"+i.toString()
            }
        }
        contentParse.push(obj);
    }

    let time = new Date();

    let article = {
        id:res.info.id,
        title:res.info.title,
        time:(time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate()),
        userInfo:user.userInfo,
        content:contentParse
    }
    
    func.setStorge("article_"+uid.toString(),article);
};

Page({
    data:{
        users:[]
    },
    onLoad(e){
        me = this;
        aid = e.id;
        uid = user.userInfo.uid;
    },
    onShow(){
        func.loading(loadData(),this);
    },
    toPreview(){
        func.navigateTo("../previewArticle/previewArticle?type=display");
    },
    reEdit(){
        func.navigateTo("../articleEdit/articleEdit?type=reEdit&aid="+aid);
    }
})